<?php

namespace app\api\controller\v1;

use app\common\controller\Api;
use think\Db;

class Classes extends Api
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';

    public function getList()
    {
        $list = Db::name("Classes")
            ->field("id, name")
            ->select();
        $this->success("获取成功", $list);
    }


    /**
     * 获取班级列表
     */
    public function getClassList()
    {
        $latitude = $this->request->get('latitude');
        $longitude = $this->request->get('longitude');
        $schoolId = $this->request->get('school_id');
        if (!$latitude || !$longitude) {
            $latitude = 22.81000000000000;
            $longitude = 108.35528564453125;
        }
        // 查询班级表和班级学生对应表
        $list = Db::name('course_table_autoplan_temp')
            ->alias('t')
            ->join('fa_course_table tb', 'tb.id=t.table_id', 'INNER')
            ->join('eb_school s', 'tb.school_id=s.id', 'INNER')
            ->join('eb_classes c', 'c.id=t.classes_id', 'INNER')
            ->join('fa_ocation o', 'o.id=t.ocation_id', 'INNER')
            ->join('fa_ocation o2', 'o.pid=o2.id', 'INNER')
            ->field("c.id,s.name as school_name,o2.name as ocation_name,c.name as className,c.cover,subjectlv_id_b,c.price,tb.start_date,o.`longitude`,o.`latitude`, (
        6371 * ACOS(
            COS(RADIANS($latitude)) * COS(RADIANS(o.latitude)) * COS(RADIANS(o.longitude) - RADIANS($longitude)) +
            SIN(RADIANS($latitude)) * SIN(RADIANS(o.latitude))
        )
    ) AS km")
            ->group("t.ocation_id,t.classes_id")
            ->where('s.id', $schoolId)
            ->order("km asc")
            ->select();

        foreach ($list as $key => $value) {
            $list[$key]['cover'] = preg_replace('/^https?\:\/\/.*\.myqcloud\.com\//', 'https://tcimg.gxyushengwenhua.com/', $value['cover']);
        }


        $total = Db::name('course_table_autoplan_temp')
            ->alias('e')
            ->find();

        return json([
            'code' => 200,
            'msg' => 'success',
            'data' => $list
        ]);
    }

    /**
     * 获取学校详情
     */
    public function getSchoolInfo()
    {
        // 假设学校信息存储在配置表或其他地方
        $schoolId = $this->request->get('school_id');
        if (!$schoolId) {
            return json([
                'code' => 400,
                'msg' => '学校ID不能为空'
            ]);
        }
        $schoolInfo = Db::name('eb_school')
            ->where('id', $schoolId)
            ->find();
        $list=$this->getClassList();
        $schoolInfo = [
            'name' => $schoolInfo['name'],
            'logo' => $schoolInfo['school_logo'],
            'description' => $schoolInfo['intro'],
            'list' =>$list->getData()['data']
        ];

        return json([
            'code' => 200,
            'msg' => 'success',
            'data' => $schoolInfo
        ]);
    }

    public function create()
    {
        $user=$this->auth->getUser();
        $data = $this->request->post();
        $classData = [
            'name' => $data['class_name'],
            'cover' => $data['class_cover'],
            'type' => $data['class_type'],
            'manager_id' => $data['class_admin'],
            'add_time' => time(),
            'create_time' => date("Y-m-d H:i:s"),
            'intro' => $data['class_description'],
            'is_apply_to_join' => $data['is_open_join'] === '是' ? 1 : 0,
            'is_check' => $data['is_verify_join'] === '是' ? 1 : 0,
            'show_in_team' => $data['is_show_in_team'] === '是' ? 1 : 0,
            'allow_see' => $data['is_member_can_view'] === '是' ? 1 : 0,
            'type' => $data['creator_type'] === '学校' ? 1 : 2, //1=学员|2=学校
            'creator_id' => $user['uid'],
        ];

        Db::name('eb_classes')->insert($classData);
        $classId = Db::name('eb_classes')->getLastInsID();

        // 假设班级管理员也是班级学生之一
        Db::name('eb_class_student')->insert([
            'classes_id' => $classId,
            'student_id' => $data['class_admin'],
            'add_time' => time(),
        ]);

        $this->success('班级创建成功');
    }

    public function classTeamList()
    {
        $user = $this->auth->getUser();
        $list = Db::name("ClassStudent")
            ->alias("cs")
            ->join("Classes c", "cs.class_id = c.id")
            ->field("c.id, c.name")
            ->where("student_id","=", $user['id'])
            ->select();
        $this->success("获取成功", $list);
    }
}
